package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.BoardMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Board;
import com.example.forum.services.IBoardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lily
 * Date: 2024-10-09
 * Time: 23:19
 */
@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {

    @Resource
    private BoardMapper boardMapper;

    @Override
    public void addOneArticleCountById(Long id) {
        if (id == null || id <= 0) {
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询对应帖子
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            //打印日志
            log.warn(ResultCode.ERROR_IS_NULL.toString() + " ,board id = " + id);
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //更新帖子数量
        Board updateboard = new Board();
        updateboard.setId(board.getId());
        updateboard.setArticleCount(board.getArticleCount() + 1);
        int row = boardMapper.updateByPrimaryKeySelective(updateboard);
        //判断受影响的⾏数
        if (row != 1) {
            log.warn(ResultCode.FAILED.toString() + ", 受影响的⾏数不等于 1 .");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public void subOneArticleCountById(Long id) {
        if (id == null || id <= 0) {
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        //查询对应帖子
        Board board = boardMapper.selectByPrimaryKey(id);
        if (board == null) {
            //打印日志
            log.warn(ResultCode.ERROR_IS_NULL.toString() + " ,board id = " + id);
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //更新帖子数量
        Board updateboard = new Board();
        updateboard.setId(board.getId());
        updateboard.setArticleCount(board.getArticleCount() - 1);

        // 判断减1之后是否小于0
        if (updateboard.getArticleCount() < 0) {
            // 如果小于0那么设置为0
            updateboard.setArticleCount(0);
        }

        // 调用DAO
        int row = boardMapper.updateByPrimaryKeySelective(updateboard);
        //判断受影响的⾏数
        if (row != 1) {
            log.warn(ResultCode.FAILED.toString() + ", 受影响的⾏数不等于 1 .");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public List<Board> selectByNum(Integer num) {
        if (num < 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 调用DAO查询数据库中的数据
        List<Board> boards = boardMapper.selectByNum(num);
        // 返回结果
        return boards;
    }

    @Override
    public Board selectById(Long id) {
        if (id == null || id <= 0) {
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        // 查询
        Board board = boardMapper.selectByPrimaryKey(id);
        // 返回结果
        return board;
    }
}
